﻿#pragma execution_character_set("utf-8")

#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QDateTime>
#include <QMessageBox>
#include <string>

using namespace std;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnSelectFile_clicked()
{
    QString filename = QFileDialog::getOpenFileName(
                        this, "选择要播放的文件",
                        "/",
                        "视频文件 (*.flv *.rmvb *.avi *.mp4);; 所有文件 (*.*);; ");

    //获取路径
    std::string dir = filename.toStdString();
    int end = dir.find_last_of("/");
    std::string str = dir.substr(0, end);

    QString path = QString::fromStdString(str);

    m_FileWatcher.addPath(path);

    //监视目录中的文件是否变化
    connect(&m_FileWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(onDirectoryChaned(QString)));

    if(!(filename.isEmpty()))
    {
        ui->lineEdit->setText(filename);

        QFileInfo file(filename);

        //文件名称
        QString _filename = tr("文件名称 ") + file.fileName();

        //文件大小，字节为单位
        qint64 size = file.size();
        int kb = size / 1024;

        QString _filesize = tr("文件大小 ") + QString::number(kb) + "k";

        QDateTime create = file.created();
        QString _createTime = tr("创建时间 ") + create.toString("yyyy-MM-dd hh:mm:ss");

        //文件的绝对路径
        QString _aPath = tr("绝对路径 ") + file.absoluteFilePath();

        //文件修改时间
        QString _time =  tr("修改时间 ") + file.lastModified().toString("yyyy-MM-dd hh:mm:ss");
        QString _readTime = tr("最后阅读时间 ") + file.lastRead().toString("yyyy-MM-dd hh:mm:ss");

        ui->plainTextEdit->appendPlainText(_filename);
        ui->plainTextEdit->appendPlainText(_filesize);
        ui->plainTextEdit->appendPlainText(_aPath);
        ui->plainTextEdit->appendPlainText(_time);
        ui->plainTextEdit->appendPlainText(_createTime);
        ui->plainTextEdit->appendPlainText(_readTime);
        ui->plainTextEdit->appendPlainText(_aPath);
    }
}

void Widget::onDirectoryChaned(QString path)
{
    QMessageBox::information(this, tr("提示"), path + tr(" 目录文件有改动"), NULL);
}

















